/*
 * play movie
 */
$(function(){
	play_index.run();	
});

var play_index = {	
		run: function(){
			this.__init__();			
			this._start_();
			this._bind_events();
		},
		
		__init__: function(){
			this.contextPath = $("#contextPath").val() || "";					
			this.itemUrl = $("#itemUrl").val();
			this.id = $("#id").val() || '';		
			this.title = $("#title").val() || '';			
			this.category = $("#category").val();
			this.website = $("#website").val();
			this.vdInstalled = vd.check();
		},
		
		_start_: function(){
			$(".oembed").oembed(null,{
			 	embedMethod: 'fill', 
				maxHeight: 504, 
				maxWidth:980,
				beforeEmbed:play_index.addWModeParameter      
			});
			
			this._hightlight_nav();
			
			//share.run();
			main.addOrUpdateViewHistory(play_index.category,play_index.title,play_index.id);
			
			side_pub._init_side_genre_decade(this);
			
			if(location.pathname.toString().indexOf("yt") > 0){
				this._get_relates();		
			}else if(location.pathname.toString().indexOf("mf") > 0){
				this._get_relates_trailer();		
			}else if(location.pathname.toString().indexOf("vo") > 0){
				this._get_relates_music();		
			}else if(location.pathname.toString().indexOf("hl") > 0){
				this._get_relates_movie();	
				
			}			
			
			//User-Agent
			var ua = navigator.userAgent;	
			
			
			/*
			if(this.website == 'Hulu'){				
				$("#play_hulu").show();
				if(location.pathname.toString().indexOf("hl") > 0){
					this._get_relates_movie();		
				}		
			}
			*/
			
			
			if(this.website == 'Moviefone'){
				if(ua.indexOf("IE") > 0 ){
					$(".ie_player").show();
				}else{
					$(".ff_player").show();
				}
			}
		
			main._play_close_side();
			var locaIndex = location.href.indexOf("#"); 
			var localUrl = location.href;
			if(locaIndex > 0){
				localUrl = localUrl.substring(0,locaIndex);
			}
			$("#fb-comments").attr("data-href", localUrl);
			$(".loadingBox").show();
		},
		
		addWModeParameter:function (oembedData) {     
            if (oembedData.type == "video") {
                    var code = oembedData.code;
                    var wmode = "transparent";
                    if (code != null && code.indexOf("wmode") < 0) {                        
                            code = code.replace("<embed ", "<param name=\"wmode\" value=\"" + wmode + "\"></param>\n<embed ");
                            code = code.replace("<embed ", "<embed wmode=\"" + wmode + "\"");    
                            code = code.replace("<embed ", "<embed allowfullscreen=\"true\"");
                          
                    }      
                    oembedData.code = code;
            }      
		},

		_get_relates: function(){						
			var url = this.contextPath + "/more/relate/" + play_index.id;		
			$.get(url, function(result){
				$("#view_other").html(result);
			});
		},		
		
		_get_relates_trailer: function(){						
			var url = this.contextPath + "/movie/trailers/relate";	
			$.get(url, function(result){
				document.getElementById('other_video').innerHTML = result;
				
			});
		},		
		_get_relates_movie: function(){						
			var url = this.contextPath + "/movie/full-length/relate/" + play_index.id;	
			$.get(url, function(result){
				document.getElementById('other_video').innerHTML = result;
				
			});
		},		
		
		_get_relates_music: function(){						
			var url = this.contextPath + "/music/video/relate/" + play_index.id;	
			$.get(url, function(result){
				document.getElementById('other_video').innerHTML = result;
				
			});
		},		
		
		_hightlight_nav: function(){
			var index = 0;		
			if(this.category == 'trailer'){
				index = 3;
			}else if(this.category == 'movie2012'){
				index = 2;
			}else if(this.category == 'full-length'){
				index = 1;
			}
			
			$("#movie_side_ul > li").eq(index).addClass("curr");
		},
		
		_bind_events: function(){			
			
			$("#genre_div > div > ul > li > a").click(function() {
				play_index.genre = $(this).attr("genre");
				play_index._genre_popular_relocation_event();
				return false;
			});

			$("#side_category_div > ul > li > a").click(function() {
				play_index.popular = $(this).attr("popular");
				play_index._genre_popular_relocation_event();
				return false;
			});
		},
		
		_genre_popular_relocation_event: function(){
			var genre = play_index.genre;
			var popular = play_index.popular;
			var params = [];
			if(genre){
				params.push("genre=" + genre);
			}
			if(popular){
				params.push("period=" + popular);
			}
			var href = location.href;
			if(href.indexOf("/music/video") > 0){
				if(!genre && !popular){
					location.href = main.contextPath + "/music/video/";
					return false;
				}
				location.href = main.contextPath + "/music/videos#" + params.join("&");
			}else if(href.indexOf("/movie/full-length") > 0){
				if(!genre && !popular){
					location.href = main.contextPath + "/movie/full-length";
					return false;
				}
				location.href = main.contextPath + "/movie/full-lengths#" + params.join("&");
			}else if(href.indexOf("/movie/trailer") > 0){
				if(!genre && !popular){
					location.href = main.contextPath + "/movie/trailer";
					return false;
				}
				location.href = main.contextPath + "/movie/trailers#" + params.join("&");
			}
		}
};

var share = {
	run: function(){
		//share btn
		$("#share_btn").click(function(){
			$("#share_fb_div").slideToggle();
			$("#share_fb_div > span.share-close").click(function(){
				$("#share_fb_div").slideUp();
			});
		});		
		
		$("#share_input").val(location.href);
		
		$("#fb_share_a").click(function(){
			var url = $("#share_input").val();
			var fbUrl = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(url);
			_gaq.push(['_trackEvent', 'Show', 'fb_share', url]);	
			window.open(fbUrl);
		});
		
		$("#tw_share_a").click(function(){
			var url = $("#share_input").val();
			var twUrl = "https://twitter.com/share?url=" +encodeURIComponent(url);
			_gaq.push(['_trackEvent', 'Show', 'twitter_share', url]);				
			window.open(twUrl);
		});
	}
};